{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "former-teens",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/find-and-replace-in-string\n",
    "\n",
    "\n",
    "Runtime: 8 ms, faster than 46.28% of C++ online submissions for Find And Replace in String.\n",
    "Memory Usage: 11.3 MB, less than 37.89% of C++ online submissions for Find And Replace in String.\n",
    "\n",
    "\n",
    "```cpp\n",
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <string>\n",
    "#include <iostream>\n",
    "#include <tuple>\n",
    "\n",
    "using namespace std;\n",
    "\n",
    "bool sort_tuple_by_second_value(const tuple<int, int> &a, const tuple<int, int> &b) {\n",
    "    return  get<1>(a) < get<1>(b);\n",
    "}\n",
    "\n",
    "class Solution {\n",
    "public:\n",
    "    string findReplaceString(string S, vector<int>& indexes, vector<string>& sources, vector<string>& targets) {\n",
    "        //7:50\n",
    "        vector<tuple<int, int>> the_order;\n",
    "        for (int i=0; i<indexes.size(); i++) {\n",
    "            the_order.push_back(make_tuple(i, indexes[i]));\n",
    "        }\n",
    "        sort(the_order.begin(), the_order.end(), sort_tuple_by_second_value);\n",
    "        vector<int> new_indexes;\n",
    "        vector<string> new_sources;\n",
    "        vector<string> new_targets;\n",
    "        for ( tuple<int, int> item : the_order) {\n",
    "            int i = get<0>(item);\n",
    "            new_indexes.push_back(indexes[i]);\n",
    "            new_sources.push_back(sources[i]);\n",
    "            new_targets.push_back(targets[i]);\n",
    "        }\n",
    "        int last_n;\n",
    "        string new_S;\n",
    "        vector<int> i_list;\n",
    "        for (int n=0; n<new_indexes.size(); n++) {\n",
    "            int i = new_indexes[n];\n",
    "            string s = new_sources[n];\n",
    "            string t = new_targets[n];\n",
    "            string possible_word = S.substr(i, s.size());\n",
    "            if (possible_word == s) {\n",
    "                if (i_list.size()==0) {\n",
    "                    new_S += S.substr(0, i) + t;\n",
    "                } else {\n",
    "                    last_n = i_list[i_list.size()-1];\n",
    "                    new_S += S.substr(new_indexes[last_n] + new_sources[last_n].size(), i-(new_indexes[last_n] + new_sources[last_n].size())) + t;\n",
    "                }\n",
    "                i_list.push_back(n);\n",
    "            }\n",
    "        }\n",
    "        if (i_list.size() != 0){\n",
    "            last_n = i_list[i_list.size()-1];\n",
    "            new_S += S.substr(new_indexes[last_n] + new_sources[last_n].size(), (S.size()) - (new_indexes[last_n] + new_sources[last_n].size()));\n",
    "        }\n",
    "        if (new_S == \"\"){\n",
    "            return S;\n",
    "        }\n",
    "        return new_S;\n",
    "        //9:23\n",
    "        //the stupid S.substr, it takes the start_index and length\n",
    "    }\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "pregnant-girlfriend",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
